package com.example.helloandroidworld;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.actionbarsherlock.app.SherlockFragment;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.helloandroidworld.MyOverlays;

public class MapFragment extends SherlockFragment 
{	
	private Button buttonToggleLocation, buttonNearestMap, buttonParkingPin, buttonSlidingView;
	LinearLayout mapContainer = null;
	public int plat, plng;
	 
	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
	{ 
		 View view = inflater.inflate(R.layout.mapfragment, container, false);

		 MapView myMap  = ((MainActivity)getSherlockActivity()).myMap;
		 MapController myMapController = myMap.getController();
		 myMapController.setZoom(17);
		 myMapController.setCenter(new GeoPoint(26306015,-98173402));
		 myMap.setClickable(true);
		 
		 if ( savedInstanceState !=null)
		 {
			 GeoPoint c = new GeoPoint(plat, plng);
			 OverlayItem overlayitem = new OverlayItem(c, "Car", "Come get me");
			 ((MainActivity)getSherlockActivity()).itemizedoverlay.addOverlay(overlayitem);
		    ((MainActivity)getSherlockActivity()).myMap.getOverlays().add(((MainActivity)getSherlockActivity()).itemizedoverlay);
		    }
		 
		 LinearLayout rl = (LinearLayout) view.findViewById(R.id.mapContainer);
		 if (myMap.getParent() == null) 
		 {
			 rl.addView(myMap);
		 }
		 mapContainer = rl;
		 setUpButtons(view);
		 return view;
		 
	}
	
	public void setUpButtons( View v)
	{
		/// set up buttons
		buttonToggleLocation = (Button) v.findViewById(R.id.onOffLocation);
		buttonNearestMap = (Button) v.findViewById(R.id.nearestMap);
		buttonParkingPin = (Button) v.findViewById(R.id.parkingPin);
		buttonSlidingView = (Button) v.findViewById(R.id.viewMapUsers);

		
		// add functionality
		buttonToggleLocation.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// stops reporting location and hides self
				// or starts reporting location
				Toast.makeText(getActivity(), "toggle works", Toast.LENGTH_SHORT).show();
			}});
		buttonNearestMap.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// find the nearest user
				// starts an activity
				
				startActivity( new Intent(getActivity(), MapNearestUser.class));
			}});
		
		
		buttonParkingPin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// starts an activity
				// adds or removes an overlay indicating position.
				plat = ((MainActivity)getSherlockActivity()).myLocation.getMyLocation().getLatitudeE6();
				plng = ((MainActivity)getSherlockActivity()).myLocation.getMyLocation().getLongitudeE6();
				
				GeoPoint c = new GeoPoint(plat, plng);
				OverlayItem overlayitem = new OverlayItem(c, "Car", "Come get me");
			    ((MainActivity)getSherlockActivity()).itemizedoverlay.addOverlay(overlayitem);
			    ((MainActivity)getSherlockActivity()).myMap.getOverlays().add(((MainActivity)getSherlockActivity()).itemizedoverlay);
				
			    
			    Toast.makeText(getActivity(), "PARKING LOCATION SET", Toast.LENGTH_SHORT).show();
			}});
		
		
		
		buttonSlidingView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AsyncLoadTasks.run(((MainActivity)getSherlockActivity()));
				//((MainActivity)getSherlockActivity()).A
				// allows the user to add or remove people from app. starts an activity
				Toast.makeText(getActivity(), "RETRIEVING USERS", Toast.LENGTH_SHORT).show();
			}});
	}
	
	@Override
	public void onDestroyView() 
	{
		MapView myMap = ((MainActivity)getSherlockActivity()).myMap;
		LinearLayout rl = mapContainer;
		if (myMap.getParent() == rl) 
		{
			rl.removeView(myMap);
		}
		super.onDestroyView();
	}
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate( savedInstanceState);
		if( savedInstanceState != null )
		{
			plat = savedInstanceState.getInt("plat");
			plng = savedInstanceState.getInt("plng");
					    
	}
			setRetainInstance(true);

        
	}
	
    public void onSaveInstanceState( Bundle saveInstanceState)
    {
    	// saves current tab during configuration changes.
    	// so it can be set properly when onCreate is called again
    	super.onSaveInstanceState(saveInstanceState);
    	saveInstanceState.putInt("plat", plat);
    	saveInstanceState.putInt("plng", plng);
    	// which is passed to onCreate()Ins
    }
	


}